package com.doudizu.shoesonline.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.doudizu.shoesonline.common.BaseResponse;
import com.doudizu.shoesonline.common.ErrorCode;
import com.doudizu.shoesonline.common.ResultUtils;
import com.doudizu.shoesonline.model.dto.size.SizeQuery;
import com.doudizu.shoesonline.model.entity.Size;
import com.doudizu.shoesonline.service.SizeService;
import com.doudizu.shoesonline.annotation.AuthCheck;
import com.doudizu.shoesonline.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ln
 */
@RestController
@RequestMapping("/size")
@Api(tags = "尺码模块")
public class SizeController {

    @Resource
    private UserService userService;

    @Resource
    private SizeService sizeService;

    /**
     *  查询所有的尺码
     */
    @GetMapping("/list")
    @ApiOperation("查询所有的尺码")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<List<Size>> list() {
        List<Size> list = sizeService.list();
        return ResultUtils.success(list);
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("尺码的分页查询")
    public BaseResponse page(SizeQuery sizeQuery) {
        return ResultUtils.success(sizeService.getSize(sizeQuery));
    }

    /**
     * 查询尺码信息，
     * @param text 输入的尺码
     * @return
     */
    @GetMapping("/search")
    @ApiOperation("查询某个尺码的信息")
    public BaseResponse getShoesMessage(String text) {
        if (StringUtils.isBlank(text)) {
            return this.list();
        }
        LambdaQueryWrapper<Size> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Size::getSize,text);
        List<Size> size = sizeService.list(lambdaQueryWrapper);
        if (size.isEmpty()) {
            return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(size);
    }


    /**
     *  添加尺码
     * @param size 尺码的信息
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加尺码")
    public BaseResponse<String> save(@RequestBody Size size) {
        sizeService.save(size);
        return ResultUtils.success("保存成功");
    }

    /**
     *  查询单个或者批量的进行修改
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "单个修改")
    public BaseResponse status(@RequestBody Size size) {
        sizeService.updateById(size);
        return ResultUtils.success(null);
    }

    /**
     * 根据id删除单个或者批量删除
     * @param ids  id集合
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation("单个或者批量删除尺码信息")
    public BaseResponse delete(@RequestParam List<Integer> ids) {
        sizeService.removeBatchByIds(ids);
        return ResultUtils.success(null);
    }

}
